package com.yvan.periscope.service;

import com.yvan.es.RestFulResult;
import com.yvan.es.SearchAction;
import com.yvan.periscope.domain.HistogramBean;
import com.yvan.platform.JsonWapper;
import io.searchbox.client.JestClient;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * Created by Blues Zhao on 下午3:18 2018/8/16.
 */

@Service
@Slf4j
public class HistogramService {

    @Autowired
    private JestClient jestClient;

    /**
     * 获取直方图数据，该方法针对periscope固定取 前缀参数_0--前缀参数_6，这7条数据
     *
     * @param indexPattern    索引
     * @param histogramPrefix 直方图前缀参数，用于决定取哪一类直方图数据
     * @param partition       分区数组
     * @param dt1             查询开始时间
     * @param dt2             查询结束时间
     * @param xAxisTitle      横坐标title
     * @param yAxisTitle      纵坐标title
     */
    public HistogramBean findHistogramFromPeriscope(String indexPattern, String histogramPrefix, long[] partition, DateTime dt1, DateTime dt2, String xAxisTitle, String yAxisTitle) throws IOException {
        SearchAction action = new SearchAction(log);
        action.index = indexPattern;
        action.size = 0;
        action.query.filter.range("@timestamp", dt1.toString(), false, dt2.toString(), true);
        action.agg.sum(histogramPrefix + "_0");
        action.agg.sum(histogramPrefix + "_1");
        action.agg.sum(histogramPrefix + "_2");
        action.agg.sum(histogramPrefix + "_3");
        action.agg.sum(histogramPrefix + "_4");
        action.agg.sum(histogramPrefix + "_5");
        action.agg.sum(histogramPrefix + "_6");
        action.agg.sum(histogramPrefix + "_m");

        RestFulResult r = action.search(jestClient);
        JsonWapper jw = r.getJsonWapper();
        Object data1 = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_0@" + histogramPrefix + "_0")).get("value");
        Object data2 = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_1@" + histogramPrefix + "_1")).get("value");
        Object data3 = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_2@" + histogramPrefix + "_2")).get("value");
        Object data4 = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_3@" + histogramPrefix + "_3")).get("value");
        Object data5 = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_4@" + histogramPrefix + "_4")).get("value");
        Object data6 = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_5@" + histogramPrefix + "_5")).get("value");
        Object data7 = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_6@" + histogramPrefix + "_6")).get("value");
        Object dataM = ((LinkedHashMap) jw.get("aggregations", "group_by@" + histogramPrefix + "_m@" + histogramPrefix + "_m")).get("value");

        HistogramBean histogramBean = new HistogramBean();
        histogramBean.addData(data1);
        histogramBean.addData(data2);
        histogramBean.addData(data3);
        histogramBean.addData(data4);
        histogramBean.addData(data5);
        histogramBean.addData(data6);
        histogramBean.addData(data7);
        histogramBean.addData(dataM);

        histogramBean.addTitleArray(partition);
        histogramBean.setXAxisTitle(xAxisTitle);
        histogramBean.setYAxisTitle(yAxisTitle);
        return histogramBean;
    }
}
